<?php
	require_once('_inc.php');

	$db	= D::$user->db();

	
	if( $_GET['act'] === 'upload' ){
		include('standard_upload.php');
		die;
	}

	if( $_GET['act'] === 'load'){		
		$id	= (int) $_POST['id'];
		$L	= $db->one("SELECT * FROM standard WHERE id=" .$id );
		$A	= @unserialize($L['sArray']);
		unset($L['sArray']);
		if( is_array($A) ) {
			foreach($A as $k=>$v){
				if( !empty($v) ){
					$L['sArray_'.$k]	= $v;
				}
			}
		}
		echo json_encode($L); 
		die;
	}

	if( $_GET['act'] === 'list'){		
		$L		= array();
		$L['root']	= $db->all("SELECT * FROM standard WHERE 1 ORDER BY 废弃 ASC, id ASC");
		$L['Total']	= count($L['root']);
		echo json_encode($L);
		die;
	}

	if( $_GET['act'] === 'save'){	
		$return	= array('success'=>true);
		$id	= (int) $_POST['id'];
		if($id){
			$db->update('standard', '动物种类,废弃,代码,标准名称,采食量,备注,sArray', 'id='. $id);
		}else{
			$db->insert('standard', '动物种类,废弃,代码,标准名称,采食量,备注,sArray');
		}
		echo json_encode($return);
		update($db);
		die;
	}



	if( $_GET['act'] === 'del'){
		if( preg_match_all("/\d+/", $_POST['dels'], $dels) ){
			$dels	= $dels[0];
			$sql	= "DELETE FROM standard WHERE id=" .join(' OR id=', $dels);
			$db->query($sql);
		}
		update($db);
		die('ok');
	}

	$units	= getUnits();

	$tpl	= D::$user->tpl();
	$tpl->assign('yesno',  file_get_contents('src/js/yesno.js'));
	$tpl->load('poultry');
	$tpl->assign('units_list', $units);
	$tpl->assign('units', json_encode($units));
	$tpl->display();

	function update(&$db){
		$path	= D::$user->path();
	}


	function &getUnits(){
		$units	= include(D::$user->path() .'/units.php');
		return $units;
	}
?>